package com.huonan.jb.tools.esuse.config;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.huonan.utils.DateHelper;

import java.util.Date;

public  class SplitTime {
        private final int threadSize;
        private final int index;
        private final String startTime;
        private final String endTime;
        private String newStartTime;
        private String newEndTime;

        public SplitTime(int threadSize, int index, String startTime, String endTime) {
            this.threadSize = threadSize;
            this.index = index;
            this.startTime = startTime;
            this.endTime = endTime;
        }

        public String getNewStartTime() {
            return newStartTime;
        }

        public String getNewEndTime() {
            return newEndTime;
        }

        public SplitTime invoke() {
            Date startDate = DateHelper.dealDateFormat(startTime);
            Date endDate = DateHelper.dealDateFormat(endTime);
            System.out.println(startDate);
            long between = DateUtil.between(startDate, endDate, DateUnit.MINUTE);
            long timeStep = between / threadSize;
            long lastTime = between % threadSize;

            Date newStartDate;
            Date newEndDate;
            if (lastTime > 0 && threadSize == index) {
                long lastStepTime = between - timeStep * (threadSize - 1);
                newStartDate = DateUtil.offsetMinute(startDate, (int) (timeStep * (index - 1)));
                newEndDate = DateUtil.offsetMinute(startDate, (int) (timeStep * (index - 1) + lastStepTime));
            } else {
                newStartDate = DateUtil.offsetMinute(startDate, (int) timeStep * (index - 1));
                newEndDate = DateUtil.offsetMinute(startDate, (int) timeStep * index);
            }
            newStartTime = DateUtil.format(newStartDate, "yyyy-MM-dd'T'HH:mm:ss");
            newEndTime = DateUtil.format(newEndDate, "yyyy-MM-dd'T'HH:mm:ss");
            return this;
        }
    }